package org.light.domain;

import java.util.Set;
import java.util.TreeSet;

import org.light.complexverb.AddUploadDomainField;
import org.light.complexverb.DomainFieldVerb;
import org.light.complexverb.UpdateUploadDomainField;
import org.light.core.PrismInterface;
import org.light.core.SpringMVCController;
import org.light.core.Verb;
import org.light.easyuilayouts.EasyUIEnumGridPagePI;
import org.light.exception.ValidateException;
import org.light.generator.MtmMybatisDaoXmlDecorator;
import org.light.generator.MybatisDaoXmlDecorator;
import org.light.generator.NamedUtilMethodGenerator;
import org.light.layouts.EasyUIGridPagePI;
import org.light.layouts.EasyUIMtmPI;
import org.light.limitedverb.CountActiveRecords;
import org.light.limitedverb.CountSearchByFieldsRecords;
import org.light.limitedverb.DaoOnlyVerb;
import org.light.limitedverb.NoControllerVerb;
import org.light.utils.StringUtil;
import org.light.verb.Activate;
import org.light.verb.ActivateAll;
import org.light.verb.Add;
import org.light.verb.CheckAccess;
import org.light.verb.Clone;
import org.light.verb.CloneAll;
import org.light.verb.Delete;
import org.light.verb.DeleteAll;
import org.light.verb.Export;
import org.light.verb.ExportPDF;
import org.light.verb.ExportPPT;
import org.light.verb.ExportWord;
import org.light.verb.FilterExcel;
import org.light.verb.FilterPDF;
import org.light.verb.FilterPPT;
import org.light.verb.FilterWord;
import org.light.verb.FindById;
import org.light.verb.FindByName;
import org.light.verb.FindFullInfoById;
import org.light.verb.ListActive;
import org.light.verb.ListAll;
import org.light.verb.SearchByFields;
import org.light.verb.SearchByFieldsByPage;
import org.light.verb.SearchIndexedNamesByShortName;
import org.light.verb.SoftDelete;
import org.light.verb.SoftDeleteAll;
import org.light.verb.Toggle;
import org.light.verb.ToggleOne;
import org.light.verb.Update;

public class MtmPrism extends Prism implements Comparable<Prism> {
	protected MtmMybatisDaoXmlDecorator mtmDecorator;
	protected MtmDao mtmDao;
	
	public MtmDao getMtmDao() {
		return mtmDao;
	}

	public void setMtmDao(MtmDao mtmDao) {
		this.mtmDao = mtmDao;
	}

	public MtmMybatisDaoXmlDecorator getMtmDecorator() {
		return mtmDecorator;
	}

	public void setMtmDecorator(MtmMybatisDaoXmlDecorator mtmDecorator) {
		this.mtmDecorator = mtmDecorator;
	}

	@Override
	public void generatePrismFiles(Boolean ignoreWarning,Boolean genUi,Boolean genController,Boolean genService,Boolean genServiceImpl,Boolean genDao,Boolean genDaoImpl) throws ValidateException {
		ValidateInfo info = this.validate(ignoreWarning);
		if (info.success(ignoreWarning) == false) {
			ValidateException e = new ValidateException(info);
			throw e;
		}
		try {
			String srcfolderPath = folderPath;
			if (this.packageToken != null && !"".equals(this.packageToken)){
				srcfolderPath = folderPath + "src/main/java/" + packagetokenToFolder(this.packageToken);

				writeToFile(srcfolderPath +packagetokenToFolder(this.domain.getDomainSuffix())  +this.getDomain().getCapFirstDomainNameWithSuffix()+ ".java",
					this.getDomain().generateClassString());
			
				if (genDao && this.getDao() != null) {
					writeToFile(
							srcfolderPath + packagetokenToFolder(this.domain.getDaoSuffix()) + StringUtil.capFirst(this.getDomain().getStandardName()) + "Dao.java",
							this.getDao().generateDaoString());
				}
	
				if (genDaoImpl&&this.getDaoImpl() != null) {
					writeToFile(
							folderPath + "src/main/resources/mapper/" + StringUtil.capFirst(this.getDomain().getStandardName()) + "Dao.xml",
							this.mtmDecorator.generateMybatisDaoXmlFileStr());
				}
	
				if (genService&&this.getService() != null) {
					writeToFile(srcfolderPath +  packagetokenToFolder(this.domain.getServiceSuffix())  + StringUtil.capFirst(this.getDomain().getStandardName())
							+ "Service.java", this.getService().generateServiceString());
				}
	
				if (genServiceImpl&&this.getServiceImpl() != null) {
					writeToFile(srcfolderPath + packagetokenToFolder(this.domain.getServiceimplSuffix()) + StringUtil.capFirst(this.getDomain().getStandardName())
							+ "ServiceImpl.java", this.getServiceImpl().generateServiceImplString());
				}
	
				if (genController&&this.controller != null) {
					writeToFile(srcfolderPath +  packagetokenToFolder(this.domain.getControllerSuffix())  + StringUtil.capFirst(this.domain.getCapFirstDomainName())
							+ this.domain.getControllerNamingSuffix()+".java", this.controller.generateControllerString());
				}
	
				if (genUi) {
					for (PrismInterface page : this.pages) {
						page.generatePIFiles(folderPath);
					}
	
					for (ManyToMany mtm : this.manyToManies) {
						EasyUIMtmPI mpage = mtm.getEuPI();
						mpage.setTechnicalStack(this.technicalStack);
						mpage.setTitles(this.getTitle(), this.getSubTitle(), this.getFooter());
						mpage.setNav(this.getNav());
						mpage.generatePIFiles(folderPath);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void generateSMEUPrismFiles(Boolean ignoreWarning,Boolean genUi,Boolean genController,Boolean genService,Boolean genServiceImpl,Boolean genDao,Boolean genDaoImpl) throws ValidateException {
		ValidateInfo info = this.validate(ignoreWarning);
		if (info.success(ignoreWarning) == false) {
			ValidateException e = new ValidateException(info);
			throw e;
		}
		try {
			String srcfolderPath = folderPath;
			if (this.packageToken != null && !"".equals(this.packageToken)){
				srcfolderPath = folderPath + "src/" + packagetokenToFolder(this.packageToken);

				writeToFile(srcfolderPath +packagetokenToFolder(this.domain.getDomainSuffix())  +this.getDomain().getCapFirstDomainNameWithSuffix()+ ".java",
					this.getDomain().generateClassString());
			
				if (genDao && this.getDao() != null) {
					writeToFile(
							srcfolderPath + packagetokenToFolder(this.domain.getDaoSuffix()) + StringUtil.capFirst(this.getDomain().getStandardName()) + "Dao.java",
							this.getDao().generateDaoString());
				}
	
				if (genDaoImpl&&this.getDaoImpl() != null) {
					writeToFile(
							srcfolderPath + packagetokenToFolder(this.domain.getDaoimplSuffix()) + StringUtil.capFirst(this.getDomain().getStandardName()) + "DummyDaoImpl.java",
							this.getDummydaoimpl().generateDaoImplString());
				}
	
				if (genService&&this.getService() != null) {
					writeToFile(srcfolderPath +  packagetokenToFolder(this.domain.getServiceSuffix())  + StringUtil.capFirst(this.getDomain().getStandardName())
							+ "Service.java", this.getService().generateServiceString());
				}
	
				if (genServiceImpl&&this.getServiceImpl() != null) {
					writeToFile(srcfolderPath + packagetokenToFolder(this.domain.getServiceimplSuffix()) + StringUtil.capFirst(this.getDomain().getStandardName())
							+ "ServiceImpl.java", this.getServiceImpl().generateServiceImplString());
				}
	
				if (genController&&this.controller != null) {
					writeToFile(srcfolderPath +  packagetokenToFolder(this.domain.getControllerSuffix())  + StringUtil.capFirst(this.domain.getCapFirstDomainName())
							+ this.domain.getControllerNamingSuffix()+".java", this.controller.generateControllerString());
				}
	
				if (genUi) {
					for (PrismInterface page : this.pages) {
						page.generatePIFiles(folderPath);
					}
	
					for (ManyToMany mtm : this.manyToManies) {
						EasyUIMtmPI mpage = mtm.getEuPI();
						mpage.setTechnicalStack(this.technicalStack);
						mpage.setTitles(this.getTitle(), this.getSubTitle(), this.getFooter());
						mpage.setNav(this.getNav());
						mpage.generatePIFiles(folderPath);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void generatePrismFromDomain(Boolean ignoreWarning) throws ValidateException, Exception {
		if (this.domain != null) {
			if (this.getPackageToken() != null) {
				this.domain.setPackageToken(packageToken);
			}
			
			this.domain.decorateCompareTo();

			this.dao = new Dao();
			this.dao.setDomain(this.domain);
			this.dao.setPackageToken(this.domain.getPackageToken());
			this.daoimpl = new DaoImpl();
			this.daoimpl.setPackageToken(this.domain.getPackageToken());
			this.daoimpl.setDomain(this.domain);
			this.daoimpl.setDao(this.dao);
			
			this.dummydaoimpl = new DummyDaoImpl();
			this.dummydaoimpl.setPackageToken(this.domain.getPackageToken());
			this.dummydaoimpl.setDomain(this.domain);

			this.service = new Service();
			this.service.setDomain(this.domain);
			this.service.setPackageToken(this.domain.getPackageToken());
			this.serviceimpl = new ServiceImpl(this.domain);
			this.serviceimpl.setPackageToken(this.domain.getPackageToken());
			this.serviceimpl.getDao().addAnnotation("Autowired");
			Method daoSetter = NamedUtilMethodGenerator.generateSetter("dao",
					new Type(this.domain.getCapFirstDomainName() + "Dao"));
			this.serviceimpl.addMethod(daoSetter);
			this.serviceimpl.addClassImports("org.springframework.beans.factory.annotation.Autowired");
			this.serviceimpl.setDomain(this.domain);
			this.serviceimpl.setService(this.service);

			Verb listAll = new ListAll(this.domain);			
			Verb update = this.domain.hasDomainId() ?new Update(this.domain):null;
			Verb delete = this.domain.hasDomainId() ?new Delete(this.domain):null;
			Verb add = this.domain.hasDomainId() ?new Add(this.domain):null;
			Verb softdelete = this.domain.hasDomainId() && this.domain.hasActiveField() ?new SoftDelete(this.domain):null;
			Verb findbyid = this.domain.hasDomainId() ? new FindById(this.domain):null;
			Verb findbyname = this.domain.hasDomainName() ? new FindByName(this.domain):null;
			Verb listactive = this.domain.hasActiveField()?new ListActive(this.domain):null;
			Verb deleteAll = this.domain.hasDomainId() ?new DeleteAll(this.domain):null;
			Verb softDeleteAll = this.domain.hasDomainId() && this.domain.hasActiveField() ?new SoftDeleteAll(this.domain):null;
			Verb toggle = this.domain.hasDomainId() && this.domain.hasActiveField() ?new Toggle(this.domain):null;
			Verb toggleOne = this.domain.hasDomainId() && this.domain.hasActiveField() ?new ToggleOne(this.domain):null;
			Verb searchByFieldsByPage = new SearchByFieldsByPage(this.domain);
			Verb activate = this.domain.hasDomainId() && this.domain.hasActiveField() ?new Activate(this.domain):null;
			Verb activateAll = this.domain.hasDomainId() && this.domain.hasActiveField() ?new ActivateAll(this.domain):null;
			Verb export = new Export(this.domain);
			Verb exportPDF = new ExportPDF(this.domain);
			Verb exportWord = new ExportWord(this.domain);
			Verb exportPPT = new ExportPPT(this.domain);
			Verb searchByFields = new SearchByFields(this.domain);
			Verb filterExcel = new FilterExcel(this.domain);
			Verb filterPDF = new FilterPDF(this.domain);
			Verb filterWord = new FilterWord(this.domain);
			Verb filterPPT = new FilterPPT(this.domain);
			Verb clone	= this.domain.hasDomainId() ?new Clone(this.domain):null;
			Verb findFullInfoById = this.domain.hasDomainId() ?new FindFullInfoById(this.domain):null;
			Verb searchIndexedNamesByShortName = this.domain.hasDomainId() ?new SearchIndexedNamesByShortName(this.domain):null;
			Verb cloneAll = this.domain.hasDomainId()  ?new CloneAll(this.domain):null;
			Verb checkAcces = new CheckAccess(this.domain);
			CountSearchByFieldsRecords countSearch = new CountSearchByFieldsRecords(this.domain);
			CountActiveRecords countActiveRecords = this.domain.hasDomainId() && this.domain.hasActiveField() ? new CountActiveRecords(this.domain):null;

			for (Field f:this.domain.getPlainFields()) {
				if (f.getFieldType().equalsIgnoreCase("image")) {
					domianFieldVerbs.add(new AddUploadDomainField(this.domain,f));
					domianFieldVerbs.add(new UpdateUploadDomainField(this.domain,f));
				}
			}
			this.addVerb(listAll);
			this.addVerb(update);
			this.addVerb(delete);
			this.addVerb(add);
			this.addVerb(softdelete);
			this.addVerb(findbyid);
			this.addVerb(findbyname);
			this.addVerb(listactive);
			this.addVerb(deleteAll);
			this.addVerb(softDeleteAll);
			this.addVerb(toggle);
			this.addVerb(toggleOne);
			this.addVerb(searchByFieldsByPage);
			this.addVerb(activate);
			this.addVerb(activateAll);
			this.addVerb(export);
			this.addVerb(exportPDF);
			this.addVerb(exportWord);
			this.addVerb(exportPPT);
			this.addVerb(searchByFields);
			this.addVerb(filterExcel);
			this.addVerb(filterPDF);
			this.addVerb(filterWord);
			this.addVerb(filterPPT);
			this.addVerb(clone);
			this.addVerb(findFullInfoById);
			this.addVerb(searchIndexedNamesByShortName);
			this.addVerb(cloneAll);
			this.addVerb(checkAcces);

			if (countSearch !=null) this.noControllerVerbs.add(countSearch);
			if (countActiveRecords !=null) this.noControllerVerbs.add(countActiveRecords);
			this.controller = new SpringMVCController(this.verbs, this.domain,ignoreWarning);
			this.controller.setPackageToken(this.packageToken);

			for (Verb v : this.verbs) {
				v.setDomain(domain);
				v.setDbType(this.dbType);
				service.addMethod(v.generateServiceMethodDefinition());
				serviceimpl.addMethod(v.generateServiceImplMethod());
				dao.addMethod(v.generateDaoMethodDefinition());
				//daoimpl.addMethod(v.generateDaoImplMethod());
				dummydaoimpl.addMethod(v.generateDummyDaoImplMethod());
				controller.addMethod(v.generateControllerMethod());
			}

			for (NoControllerVerb nVerb : this.noControllerVerbs) {
				nVerb.setDomain(domain);
				service.addMethod(nVerb.generateServiceMethodDefinition());
				serviceimpl.addMethod(nVerb.generateServiceImplMethod());
				dao.addMethod(nVerb.generateDaoMethodDefinition());
				//daoimpl.addMethod(nVerb.generateDaoImplMethod());
				dummydaoimpl.addMethod(nVerb.generateDummyDaoImplMethod());
			}

			for (DaoOnlyVerb oVerb : this.daoOnlyVerbs) {
				oVerb.setDomain(domain);
				dao.addMethod(oVerb.generateDaoMethodDefinition());
				//daoimpl.addMethod(oVerb.generateDaoImplMethod());
				dummydaoimpl.addMethod(oVerb.generateDummyDaoImplMethod());
			}
			
			for (DomainFieldVerb dfv:domianFieldVerbs) {
				this.controller.addMethod(dfv.generateControllerMethod());
			}

			this.mybatisDaoXmlDecorator = new MybatisDaoXmlDecorator();
			this.mybatisDaoXmlDecorator.setDomain(this.getDomain());
			
			EasyUIGridPagePI easyui = new EasyUIGridPagePI(this.domain);
			easyui.setTitles(this.getTitle(),this.getSubTitle(),this.getFooter());
			easyui.setDomain(this.domain);
			easyui.setTechnicalStack(this.technicalStack);
			this.addPage(easyui);

			if (this.domain.manyToManies != null && this.domain.manyToManies.size() > 0) {
				for (ManyToMany mtm : this.domain.manyToManies) {
					String slaveName = mtm.getManyToManySalveName();
					String masterName = this.domain.getStandardName();
					if (setContainsDomain(this.projectDomains, masterName)
							&& setContainsDomain(this.projectDomains, slaveName)) {
						Domain tempo = lookupDoaminInSet(this.projectDomains, slaveName);
						Domain myslave = tempo==null?null:(Domain)tempo.deepClone();
						if (myslave == null) continue;
						if (!StringUtil.isBlank(mtm.getSlaveAlias())){
							myslave.setAlias(mtm.getSlaveAlias());
							myslave.setAliasLabel(mtm.getSlaveAliasLabel());
						}
						ManyToMany mymtm = new ManyToMany(lookupDoaminInSet(this.projectDomains, masterName),
								myslave,mtm.getMasterValue(),mtm.getValues());	
						mymtm.setSlaveAlias(myslave.getAlias());
						mymtm.setSlaveAliasLabel(mtm.getSlaveAliasLabel());
						mymtm.setValues(mtm.getValues());
						this.manyToManies.add(mymtm);
					} else {
						ValidateInfo validateInfo = new ValidateInfo();
						validateInfo.addCompileError("棱柱" + this.getText() + "多对多设置有误。");
						ValidateException em = new ValidateException(validateInfo);
						throw em;
					}
				}
			}
			for (ManyToMany mtm : this.manyToManies) {
				mtm.setTitle(this.title);
				mtm.setSubTitle(this.subTitle);
				mtm.setFooter(this.footer);
				mtm.setCrossOrigin(this.crossOrigin);
				this.service.addMethod(mtm.getAssign().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getAssign().generateServiceImplMethod());
				this.dao.addMethod(mtm.getAssign().generateDaoMethodDefinition());
				//this.daoimpl.addMethod(mtm.getAssign().generateDaoImplMethod());
				//this.dummydaoimpl.addMethod(mtm.getAssign().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getAssign().generateDaoImplMethod());
				this.controller.addMethod(mtm.getAssign().generateControllerMethod());

				this.service.addMethod(mtm.getRevoke().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getRevoke().generateServiceImplMethod());
				this.dao.addMethod(mtm.getRevoke().generateDaoMethodDefinition());
				//this.daoimpl.addMethod(mtm.getRevoke().generateDaoImplMethod());
				//this.dummydaoimpl.addMethod(mtm.getRevoke().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getRevoke().generateDaoImplMethod());
				this.controller.addMethod(mtm.getRevoke().generateControllerMethod());

				this.service.addMethod(mtm.getListMyActive().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getListMyActive().generateServiceImplMethod());
				this.dao.addMethod(mtm.getListMyActive().generateDaoMethodDefinition());
				//this.daoimpl.addMethod(mtm.getListMyActive().generateDaoImplMethod());
				//this.dummydaoimpl.addMethod(mtm.getListMyActive().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addResultMap(mtm.getSlave());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getListMyActive().generateDaoImplMethod());
				this.controller.addMethod(mtm.getListMyActive().generateControllerMethod());

				this.service.addMethod(mtm.getListMyAvailableActive().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getListMyAvailableActive().generateServiceImplMethod());
				this.dao.addMethod(mtm.getListMyAvailableActive().generateDaoMethodDefinition());
				//this.daoimpl.addMethod(mtm.getListMyAvailableActive().generateDaoImplMethod());
				//this.dummydaoimpl.addMethod(mtm.getListMyAvailableActive().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getListMyAvailableActive().generateDaoImplMethod());
				this.controller.addMethod(mtm.getListMyAvailableActive().generateControllerMethod());
				//mtm.slave.decorateCompareTo();
				
				Service slaveService = new Service();
				slaveService.setDomain(mtm.slave);
				slaveService.addAnnotation("Autowired");
				slaveService.setStandardName(mtm.slave.getCapFirstDomainName() + "Service");
				
				Method slaveServiceSetter = NamedUtilMethodGenerator.generateSetter(mtm.slave.getLowerFirstDomainName()+"Service",
						new Type(mtm.slave.getCapFirstDomainName() + "Service",mtm.getSlave().getPackageToken()+"."+mtm.slave.getServiceSuffix()+"."+mtm.slave.getCapFirstDomainName() + "Service"));
				this.serviceimpl.addMethod(slaveServiceSetter);
				this.serviceimpl.addOtherService(slaveService);
			}
		}
	}
	
	public void generateEnumPrismFromEnum(Boolean ignoreWarning) throws ValidateException, Exception {
		if (this.domain != null && this.domain instanceof Enum) {
			if (this.domain.getDbType().equalsIgnoreCase("oracle")) {
				generateEnumOraclePrismFromEnum(ignoreWarning);
			}else {
				if (this.getPackageToken() != null) {
					this.domain.setPackageToken(packageToken);
				}
				
				this.domain.decorateCompareTo();
	
				this.dao = new Dao();
				this.dao.setDomain(this.domain);
				this.dao.setPackageToken(this.domain.getPackageToken());
				this.daoimpl = new DaoImpl();
				this.daoimpl.setPackageToken(this.domain.getPackageToken());
				this.daoimpl.setDomain(this.domain);
				this.daoimpl.setDao(this.dao);
				
				this.dummydaoimpl = new DummyDaoImpl();
				this.dummydaoimpl.setPackageToken(this.domain.getPackageToken());
				this.dummydaoimpl.setDomain(this.domain);
	
				this.service = new Service();
				this.service.setDomain(this.domain);
				this.service.setPackageToken(this.domain.getPackageToken());
				this.serviceimpl = new ServiceImpl(this.domain);
				this.serviceimpl.setPackageToken(this.domain.getPackageToken());
				this.serviceimpl.getDao().addAnnotation("Autowired");
				Method daoSetter = NamedUtilMethodGenerator.generateSetter("dao",
						new Type(this.domain.getCapFirstDomainName() + "Dao"));
				this.serviceimpl.addMethod(daoSetter);
				this.serviceimpl.addClassImports("org.springframework.beans.factory.annotation.Autowired");
				this.serviceimpl.setDomain(this.domain);
				this.serviceimpl.setService(this.service);
	
				Verb listAll = new ListAll(this.domain);			
				Verb findbyid = this.domain.hasDomainId() ? new FindById(this.domain):null;
				Verb findbyname = this.domain.hasDomainName() ? new FindByName(this.domain):null;
				Verb listactive = this.domain.hasActiveField()?new ListActive(this.domain):null;
				Verb searchByFieldsByPage = new SearchByFieldsByPage(this.domain);
				Verb export = new Export(this.domain);
				Verb exportPDF = new ExportPDF(this.domain);
				Verb exportWord = new ExportWord(this.domain);
				Verb exportPPT = new ExportPPT(this.domain);
				Verb searchByFields = new SearchByFields(this.domain);
				Verb filterExcel = new FilterExcel(this.domain);
				Verb filterPDF = new FilterPDF(this.domain);
				Verb filterWord = new FilterWord(this.domain);
				Verb filterPPT = new FilterPPT(this.domain);
				Verb findFullInfoById = this.domain.hasDomainId() ?new FindFullInfoById(this.domain):null;
				Verb searchIndexedNamesByShortName = this.domain.hasDomainId() ?new SearchIndexedNamesByShortName(this.domain):null;
				Verb checkAcces = new CheckAccess(this.domain);
				CountSearchByFieldsRecords countSearch = new CountSearchByFieldsRecords(this.domain);
				CountActiveRecords countActiveRecords = this.domain.hasDomainId() && this.domain.hasActiveField() ? new CountActiveRecords(this.domain):null;
	
				for (Field f:this.domain.getPlainFields()) {
					if (f.getFieldType().equalsIgnoreCase("image")) {
						domianFieldVerbs.add(new AddUploadDomainField(this.domain,f));
						domianFieldVerbs.add(new UpdateUploadDomainField(this.domain,f));
					}
				}
				this.addVerb(listAll);
				this.addVerb(findbyid);
				this.addVerb(findbyname);
				this.addVerb(listactive);
				this.addVerb(searchByFieldsByPage);
				this.addVerb(export);
				this.addVerb(exportPDF);
				this.addVerb(exportWord);
				this.addVerb(exportPPT);
				this.addVerb(searchByFields);
				this.addVerb(filterExcel);
				this.addVerb(filterPDF);
				this.addVerb(filterWord);
				this.addVerb(filterPPT);
				this.addVerb(findFullInfoById);
				this.addVerb(searchIndexedNamesByShortName);
				this.addVerb(checkAcces);

				if (countSearch !=null) this.noControllerVerbs.add(countSearch);
				if (countActiveRecords !=null) this.noControllerVerbs.add(countActiveRecords);
				this.controller = new SpringMVCController(this.verbs, this.domain,ignoreWarning);
				this.controller.setPackageToken(this.packageToken);
	
				for (Verb v : this.verbs) {
					v.setDomain(domain);
					v.setDbType(this.dbType);
					service.addMethod(v.generateServiceMethodDefinition());
					serviceimpl.addMethod(v.generateServiceImplMethod());
					dao.addMethod(v.generateDaoMethodDefinition());
					//daoimpl.addMethod(v.generateDaoImplMethod());
					dummydaoimpl.addMethod(v.generateDummyDaoImplMethod());
					controller.addMethod(v.generateControllerMethod());
				}
	
				for (NoControllerVerb nVerb : this.noControllerVerbs) {
					nVerb.setDomain(domain);
					service.addMethod(nVerb.generateServiceMethodDefinition());
					serviceimpl.addMethod(nVerb.generateServiceImplMethod());
					dao.addMethod(nVerb.generateDaoMethodDefinition());
					//daoimpl.addMethod(nVerb.generateDaoImplMethod());
					dummydaoimpl.addMethod(nVerb.generateDummyDaoImplMethod());
				}
	
				for (DaoOnlyVerb oVerb : this.daoOnlyVerbs) {
					oVerb.setDomain(domain);
					dao.addMethod(oVerb.generateDaoMethodDefinition());
					//daoimpl.addMethod(oVerb.generateDaoImplMethod());
					dummydaoimpl.addMethod(oVerb.generateDummyDaoImplMethod());
				}
				
				for (DomainFieldVerb dfv:domianFieldVerbs) {
					this.controller.addMethod(dfv.generateControllerMethod());
				}
	
				this.mybatisDaoXmlDecorator = new MybatisDaoXmlDecorator();
				this.mybatisDaoXmlDecorator.setDomain(this.getDomain());
				Set<Domain> resultMaps = new TreeSet<Domain>();
				resultMaps.add(this.domain);
				this.mybatisDaoXmlDecorator.setResultMaps(resultMaps);
	
				EasyUIEnumGridPagePI easyui = new EasyUIEnumGridPagePI(this.domain);
				easyui.setTitles(this.getTitle(),this.getSubTitle(),this.getFooter());
				easyui.setDomain(this.domain);
				easyui.setTechnicalStack(this.technicalStack);
				this.addPage(easyui);
	
				if (this.domain.manyToManies != null && this.domain.manyToManies.size() > 0) {
					for (ManyToMany mtm : this.domain.manyToManies) {
						String slaveName = mtm.getManyToManySalveName();
						String masterName = this.domain.getStandardName();
						if (setContainsDomain(this.projectDomains, masterName)
								&& setContainsDomain(this.projectDomains, slaveName)) {
							Domain tempo = lookupDoaminInSet(this.projectDomains, slaveName);
							Domain myslave = tempo==null?null:(Domain)tempo.deepClone();
							if (myslave == null) continue;
							if (!StringUtil.isBlank(mtm.getSlaveAlias())){
								myslave.setAlias(mtm.getSlaveAlias());
								myslave.setAliasLabel(mtm.getSlaveAliasLabel());
							}
							ManyToMany mymtm = new ManyToMany(lookupDoaminInSet(this.projectDomains, masterName),
									myslave,mtm.getMasterValue(),mtm.getValues());	
							mymtm.setSlaveAlias(myslave.getAlias());
							mymtm.setSlaveAliasLabel(mtm.getSlaveAliasLabel());
							mymtm.setValues(mtm.getValues());
							this.manyToManies.add(mymtm);
						} else {
							ValidateInfo validateInfo = new ValidateInfo();
							validateInfo.addCompileError("棱柱" + this.getText() + "多对多设置有误。");
							ValidateException em = new ValidateException(validateInfo);
							throw em;
						}
					}
				}
				for (ManyToMany mtm : this.manyToManies) {
					mtm.setTitle(this.title);
					mtm.setSubTitle(this.subTitle);
					mtm.setFooter(this.footer);
					mtm.setCrossOrigin(this.crossOrigin);
					this.service.addMethod(mtm.getAssign().generateServiceMethodDefinition());
					this.serviceimpl.addMethod(mtm.getAssign().generateServiceImplMethod());
					this.dao.addMethod(mtm.getAssign().generateDaoMethodDefinition());
					//this.daoimpl.addMethod(mtm.getAssign().generateDaoImplMethod());
					//this.dummydaoimpl.addMethod(mtm.getAssign().generateDummyDaoImplMethod());
					this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getAssign().generateDaoImplMethod());
					this.controller.addMethod(mtm.getAssign().generateControllerMethod());
	
					this.service.addMethod(mtm.getRevoke().generateServiceMethodDefinition());
					this.serviceimpl.addMethod(mtm.getRevoke().generateServiceImplMethod());
					this.dao.addMethod(mtm.getRevoke().generateDaoMethodDefinition());
					//this.daoimpl.addMethod(mtm.getRevoke().generateDaoImplMethod());
					//this.dummydaoimpl.addMethod(mtm.getRevoke().generateDummyDaoImplMethod());
					this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getRevoke().generateDaoImplMethod());
					this.controller.addMethod(mtm.getRevoke().generateControllerMethod());
	
					this.service.addMethod(mtm.getListMyActive().generateServiceMethodDefinition());
					this.serviceimpl.addMethod(mtm.getListMyActive().generateServiceImplMethod());
					this.dao.addMethod(mtm.getListMyActive().generateDaoMethodDefinition());
					//this.daoimpl.addMethod(mtm.getListMyActive().generateDaoImplMethod());
					//this.dummydaoimpl.addMethod(mtm.getListMyActive().generateDummyDaoImplMethod());
					this.mybatisDaoXmlDecorator.addResultMap(mtm.getSlave());
					this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getListMyActive().generateDaoImplMethod());
					this.controller.addMethod(mtm.getListMyActive().generateControllerMethod());
	
					this.service.addMethod(mtm.getListMyAvailableActive().generateServiceMethodDefinition());
					this.serviceimpl.addMethod(mtm.getListMyAvailableActive().generateServiceImplMethod());
					this.dao.addMethod(mtm.getListMyAvailableActive().generateDaoMethodDefinition());
					//this.daoimpl.addMethod(mtm.getListMyAvailableActive().generateDaoImplMethod());
					//this.dummydaoimpl.addMethod(mtm.getListMyAvailableActive().generateDummyDaoImplMethod());
					this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getListMyAvailableActive().generateDaoImplMethod());
					this.controller.addMethod(mtm.getListMyAvailableActive().generateControllerMethod());
					//mtm.slave.decorateCompareTo();
					
					Service slaveService = new Service();
					slaveService.setDomain(mtm.slave);
					slaveService.addAnnotation("Autowired");
					slaveService.setStandardName(mtm.slave.getCapFirstDomainName() + "Service");
					
					Method slaveServiceSetter = NamedUtilMethodGenerator.generateSetter(mtm.slave.getLowerFirstDomainName()+"Service",
							new Type(mtm.slave.getCapFirstDomainName() + "Service",mtm.getSlave().getPackageToken()+"."+mtm.slave.getServiceSuffix()+"."+mtm.slave.getCapFirstDomainName() + "Service"));
					this.serviceimpl.addMethod(slaveServiceSetter);
					this.serviceimpl.addOtherService(slaveService);
				}
			}
		}
	}
	
	public void generateEnumOraclePrismFromEnum(Boolean ignoreWarning) throws ValidateException, Exception {
		if (this.domain != null && this.domain instanceof Enum) {
			if (this.getPackageToken() != null) {
				this.domain.setPackageToken(packageToken);
			}
			
			this.domain.decorateCompareTo();

			this.dao = new Dao();
			this.dao.setDomain(this.domain);
			this.dao.setPackageToken(this.domain.getPackageToken());
			this.daoimpl = new DaoImpl();
			this.daoimpl.setPackageToken(this.domain.getPackageToken());
			this.daoimpl.setDomain(this.domain);
			this.daoimpl.setDao(this.dao);
			
			this.dummydaoimpl = new DummyDaoImpl();
			this.dummydaoimpl.setPackageToken(this.domain.getPackageToken());
			this.dummydaoimpl.setDomain(this.domain);

			this.service = new Service();
			this.service.setDomain(this.domain);
			this.service.setPackageToken(this.domain.getPackageToken());
			this.serviceimpl = new ServiceImpl(this.domain);
			this.serviceimpl.setPackageToken(this.domain.getPackageToken());
			this.serviceimpl.getDao().addAnnotation("Autowired");
			Method daoSetter = NamedUtilMethodGenerator.generateSetter("dao",
					new Type(this.domain.getCapFirstDomainName() + "Dao"));
			this.serviceimpl.addMethod(daoSetter);
			this.serviceimpl.addClassImports("org.springframework.beans.factory.annotation.Autowired");
			this.serviceimpl.setDomain(this.domain);
			this.serviceimpl.setService(this.service);

			Verb listAll = new org.light.oracle.oracleverb.ListAll(this.domain);			
			Verb findbyid = this.domain.hasDomainId() ? new org.light.oracle.oracleverb.FindById(this.domain):null;
			Verb findbyname = this.domain.hasDomainName() ? new org.light.oracle.oracleverb.FindByName(this.domain):null;
			Verb listactive = this.domain.hasActiveField()?new org.light.oracle.oracleverb.ListActive(this.domain):null;
			Verb searchByFieldsByPage = new org.light.oracle.oracleverb.SearchByFieldsByPage(this.domain);
			Verb export = new Export(this.domain);
			Verb exportPDF = new ExportPDF(this.domain);
			Verb exportWord = new ExportWord(this.domain);
			Verb exportPPT = new ExportPPT(this.domain);
			Verb searchByFields = new org.light.oracle.oracleverb.SearchByFields(this.domain);
			Verb filterExcel = new FilterExcel(this.domain);
			Verb filterPDF = new FilterPDF(this.domain);
			Verb filterWord = new FilterWord(this.domain);
			Verb filterPPT = new FilterPPT(this.domain);
			Verb findFullInfoById = this.domain.hasDomainId() ?new FindFullInfoById(this.domain):null;
			Verb searchIndexedNamesByShortName = this.domain.hasDomainId() ?new SearchIndexedNamesByShortName(this.domain):null;
			Verb checkAcces = new CheckAccess(this.domain);

			org.light.oracle.limitedverb.CountSearchByFieldsRecords countSearch = new org.light.oracle.limitedverb.CountSearchByFieldsRecords(this.domain);
			org.light.oracle.limitedverb.CountActiveRecords countActiveRecords = this.domain.hasDomainId() && this.domain.hasActiveField() ? new org.light.oracle.limitedverb.CountActiveRecords(this.domain):null;

			for (Field f:this.domain.getPlainFields()) {
				if (f.getFieldType().equalsIgnoreCase("image")) {
					domianFieldVerbs.add(new AddUploadDomainField(this.domain,f));
					domianFieldVerbs.add(new UpdateUploadDomainField(this.domain,f));
				}
			}
			this.addVerb(listAll);
			this.addVerb(findbyid);
			this.addVerb(findbyname);
			this.addVerb(listactive);
			this.addVerb(searchByFieldsByPage);
			this.addVerb(export);
			this.addVerb(exportPDF);
			this.addVerb(exportWord);
			this.addVerb(exportPPT);
			this.addVerb(searchByFields);
			this.addVerb(filterExcel);
			this.addVerb(filterPDF);
			this.addVerb(filterWord);
			this.addVerb(filterPPT);
			this.addVerb(findFullInfoById);
			this.addVerb(searchIndexedNamesByShortName);
			this.addVerb(checkAcces);

			if (countSearch !=null) this.noControllerVerbs.add(countSearch);
			if (countActiveRecords !=null) this.noControllerVerbs.add(countActiveRecords);
			this.controller = new SpringMVCController(this.verbs, this.domain,ignoreWarning);
			this.controller.setPackageToken(this.packageToken);

			for (Verb v : this.verbs) {
				v.setDomain(domain);
				v.setDbType(this.dbType);
				service.addMethod(v.generateServiceMethodDefinition());
				serviceimpl.addMethod(v.generateServiceImplMethod());
				dao.addMethod(v.generateDaoMethodDefinition());
				daoimpl.addMethod(v.generateDaoImplMethod());
				dummydaoimpl.addMethod(v.generateDummyDaoImplMethod());
				controller.addMethod(v.generateControllerMethod());
			}

			for (NoControllerVerb nVerb : this.noControllerVerbs) {
				nVerb.setDomain(domain);
				service.addMethod(nVerb.generateServiceMethodDefinition());
				serviceimpl.addMethod(nVerb.generateServiceImplMethod());
				dao.addMethod(nVerb.generateDaoMethodDefinition());
				daoimpl.addMethod(nVerb.generateDaoImplMethod());
				dummydaoimpl.addMethod(nVerb.generateDummyDaoImplMethod());
			}

			for (DaoOnlyVerb oVerb : this.daoOnlyVerbs) {
				oVerb.setDomain(domain);
				dao.addMethod(oVerb.generateDaoMethodDefinition());
				daoimpl.addMethod(oVerb.generateDaoImplMethod());
				dummydaoimpl.addMethod(oVerb.generateDummyDaoImplMethod());
			}
			
			for (DomainFieldVerb dfv:domianFieldVerbs) {
				this.controller.addMethod(dfv.generateControllerMethod());
			}

			this.mybatisDaoXmlDecorator = new MybatisDaoXmlDecorator();
			this.mybatisDaoXmlDecorator.setDomain(this.getDomain());
			Set<Domain> resultMaps = new TreeSet<Domain>();
			resultMaps.add(this.domain);
			this.mybatisDaoXmlDecorator.setResultMaps(resultMaps);
			Set<Method> daoimplMethods = new TreeSet<Method>();
			for (Verb vb : this.verbs) {
				if (vb!=null && vb.generateDaoImplMethod()!=null){
					daoimplMethods.add(vb.generateDaoImplMethod());
				}
			}
			for (DaoOnlyVerb dovb : this.daoOnlyVerbs) {
				if (dovb!=null && dovb.generateDaoImplMethod()!=null){
					daoimplMethods.add(dovb.generateDaoImplMethod());
				}
			}
			for (NoControllerVerb ncvb : this.noControllerVerbs) {
				if (ncvb!=null && ncvb.generateDaoImplMethod()!=null){
					daoimplMethods.add(ncvb.generateDaoImplMethod());
				}
			}
			this.mybatisDaoXmlDecorator.setDaoXmlMethods(daoimplMethods);

			EasyUIEnumGridPagePI easyui = new EasyUIEnumGridPagePI(this.domain);
			easyui.setTitles(this.getTitle(),this.getSubTitle(),this.getFooter());
			easyui.setDomain(this.domain);
			easyui.setTechnicalStack(this.technicalStack);
			this.addPage(easyui);

			if (this.domain.manyToManies != null && this.domain.manyToManies.size() > 0) {
				for (ManyToMany mtm : this.domain.manyToManies) {
					String slaveName = mtm.getManyToManySalveName();
					String masterName = this.domain.getStandardName();
					if (setContainsDomain(this.projectDomains, masterName)
							&& setContainsDomain(this.projectDomains, slaveName)) {
						Domain tempo = lookupDoaminInSet(this.projectDomains, slaveName);
						Domain myslave = tempo==null?null:(Domain)tempo.deepClone();
						if (myslave == null) continue;
						if (!StringUtil.isBlank(mtm.getSlaveAlias())){
							myslave.setAlias(mtm.getSlaveAlias());
							myslave.setAliasLabel(mtm.getSlaveAliasLabel());
						}
						ManyToMany mymtm = new ManyToMany(lookupDoaminInSet(this.projectDomains, masterName),
								myslave,mtm.getMasterValue(),mtm.getValues());	
						mymtm.setSlaveAlias(myslave.getAlias());
						mymtm.setSlaveAliasLabel(mtm.getSlaveAliasLabel());
						mymtm.setValues(mtm.getValues());
						this.manyToManies.add(mymtm);
					} else {
						ValidateInfo validateInfo = new ValidateInfo();
						validateInfo.addCompileError("棱柱" + this.getText() + "多对多设置有误。");
						ValidateException em = new ValidateException(validateInfo);
						throw em;
					}
				}
			}
			for (ManyToMany mtm : this.manyToManies) {
				mtm.setTitle(this.title);
				mtm.setSubTitle(this.subTitle);
				mtm.setFooter(this.footer);
				mtm.setCrossOrigin(this.crossOrigin);
				this.service.addMethod(mtm.getAssign().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getAssign().generateServiceImplMethod());
				this.dao.addMethod(mtm.getAssign().generateDaoMethodDefinition());
				this.daoimpl.addMethod(mtm.getAssign().generateDaoImplMethod());
				this.dummydaoimpl.addMethod(mtm.getAssign().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getAssign().generateDaoImplMethod());
				this.controller.addMethod(mtm.getAssign().generateControllerMethod());

				this.service.addMethod(mtm.getRevoke().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getRevoke().generateServiceImplMethod());
				this.dao.addMethod(mtm.getRevoke().generateDaoMethodDefinition());
				this.daoimpl.addMethod(mtm.getRevoke().generateDaoImplMethod());
				this.dummydaoimpl.addMethod(mtm.getRevoke().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getRevoke().generateDaoImplMethod());
				this.controller.addMethod(mtm.getRevoke().generateControllerMethod());

				this.service.addMethod(mtm.getListMyActive().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getListMyActive().generateServiceImplMethod());
				this.dao.addMethod(mtm.getListMyActive().generateDaoMethodDefinition());
				this.daoimpl.addMethod(mtm.getListMyActive().generateDaoImplMethod());
				this.dummydaoimpl.addMethod(mtm.getListMyActive().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addResultMap(mtm.getSlave());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getListMyActive().generateDaoImplMethod());
				this.controller.addMethod(mtm.getListMyActive().generateControllerMethod());

				this.service.addMethod(mtm.getListMyAvailableActive().generateServiceMethodDefinition());
				this.serviceimpl.addMethod(mtm.getListMyAvailableActive().generateServiceImplMethod());
				this.dao.addMethod(mtm.getListMyAvailableActive().generateDaoMethodDefinition());
				this.daoimpl.addMethod(mtm.getListMyAvailableActive().generateDaoImplMethod());
				this.dummydaoimpl.addMethod(mtm.getListMyAvailableActive().generateDummyDaoImplMethod());
				this.mybatisDaoXmlDecorator.addDaoXmlMethod(mtm.getListMyAvailableActive().generateDaoImplMethod());
				this.controller.addMethod(mtm.getListMyAvailableActive().generateControllerMethod());
				//mtm.slave.decorateCompareTo();
				
				Service slaveService = new Service();
				slaveService.setDomain(mtm.slave);
				slaveService.addAnnotation("Autowired");
				slaveService.setStandardName(mtm.slave.getCapFirstDomainName() + "Service");
				
				Method slaveServiceSetter = NamedUtilMethodGenerator.generateSetter(mtm.slave.getLowerFirstDomainName()+"Service",
						new Type(mtm.slave.getCapFirstDomainName() + "Service",mtm.getSlave().getPackageToken()+"."+mtm.slave.getServiceSuffix()+"."+mtm.slave.getCapFirstDomainName() + "Service"));
				this.serviceimpl.addMethod(slaveServiceSetter);
				this.serviceimpl.addOtherService(slaveService);
			}
		}
	}

	@Override
	public int compareTo(Prism o) {
		String myName = this.getStandardName();
		String otherName = o.getStandardName();
		if (StringUtil.isBlank(myName)) myName = "";
		if (StringUtil.isBlank(otherName)) otherName = "";
		return myName.compareTo(otherName);
	}

	@Override
	public boolean equals(Object o) {
		return (this.compareTo((Prism) o) == 0);
	}

}
